<?php
/*
 * Created on 2012-9-6
 * Creater :cnluckylee
 * Creater mail:cnlucklee@gmail.com
 */
  class Stat_model extends CI_Model{

	private $_date ;
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('user_agent');
 		$this->load->model('anti_model');
 		$this->_date = date('Y-m-d');
 	}
 	//写入访问记录数据库
 	public function insertdata()
 	{
 		$data_tmp = Tools::url2Array();
 		/*url数据整理*/
 		$data = array();
 		$wid = $data['wid'] = $data_tmp['wid'];
 		$data['refer'] = $data_tmp['refer'];
 		$data['arriveCount'] = $data_tmp['arriveCount'];
 		$data['arriveRefer'] = $data_tmp['arriveRefer'];
		$data['arriveTime'] = time();
 		$tj_eid =  trim($this->input->get("tj_eid"));
 		$eid = explode('-',$tj_eid);
		$vid = $data['vid'] = intval($eid[0]);
 		$data_tmp['firstTime'] = $data['firstTime'] = intval($eid[1]);
 		$url = $data_tmp['refer'];   //获取完整的来路URL
 		if($url)
 		{
 			$str = str_replace("http://","",$url);  //去掉http://
			$strdomain = explode("/",$str);               // 以“/”分开成数组
			$data['refer_domain']   = trim($strdomain[0]);              //取第一个“/”以前的字符
 		}
		$uid = intval($data_tmp['uid']);
 		$ip = $data['ip'] =  $this->input->ip_address();

 		/*解析ip地址*/
 		include_once(APPPATH.'libraries/geoipcity/geoipcity.inc');
 		include_once(APPPATH.'libraries/geoipcity/geoipregionvars.php');
 		$gi = geoip_open(APPPATH."libraries/geoipcity/GeoLiteCity.dat",GEOIP_STANDARD);
		$record = geoip_record_by_addr($gi,$data['ip']);
		if($record)
		{
	 		$data_tmp['city'] = $data['city'] = $record->city;
	 		$data_tmp['province'] = $data['province'] = $GEOIP_REGION_NAME[$record->country_code][$record->region];
 			$data_tmp['region'] = $data['region'] = $record->region;
		}
		self::Vistor($wid,$vid,$data_tmp);
		self::IP($wid,$ip);
		self::webvistcount($wid);
 		$data['stopTime'] = intval($data_tmp['st']);
 		unset($data_tmp);
		/*检查是否是从别的网站过来的*/
 		if ($data['refer'])
		{
		    $search_keyword =  Tools::getKeywordBySearch($data['refer'],$data['wid']);
			$data['searchEngine'] = $search_keyword['searchEngine'];
			$data['keyword'] = $search_keyword['keyword'];
		}
		/*检测是否是本站会员*/
		$data_new = array();
 		$data_new = self::checkWebsite($data['wid'],$data['vid'],$uid,$data);

		if(empty($data_new))
		{
			return ;
		}
		unset($data_new['bad_count'],$data_new['eyi_state']);
		$this->redisinit->push($this->config->item('stat').$wid."::".$this->_date,$data_new);
		return null;
 	}

/**
 * 记录网站被访问次数
 * 记录形式 wid::2013-04-05
 */
 	public function webvistcount($wid)
 	{
 		//自增
 		$this->redisinit->increment($this->config->item('stat').$wid);
 	}

 	/**
 	 * 访客处理函数
 	 */
 	public function Vistor($wid,$vid,$data)
 	{
 		$vister = array();
 		$vister['browser'] = $this->agent->browser();
 		$vister['browser_ver'] = $this->agent->version();
 		$vister['OS'] = $this->agent->platform();
 		$vister['browser_Name'] = $vister['browser'].' '.$vister['browser_ver'];
 		$vister['OSLang'] = $data['lg'];
 		$vister['fenbianlv'] = $data['showp'];
 		$vister['userAgent'] = $this->agent->agent_string();
 		/*
 		 * 记录访客
 		 * 如果存在则访客数＋1，否则独立访客数和访客数都＋1
 		 *
 		 */
		if($this->redisinit->hsetnx($this->config->item('vister').$wid,$vid,$vister))
		{
			$this->redisinit->hsetnx($this->config->item('dayvister').$this->_date."::".$wid,$vid,$vister);
		}else{
			$this->redisinit->hsetnx($this->config->item('indevister').$this->_date."::".$wid,$vid,$vister);
			$this->redisinit->hsetnx($this->config->item('dayvister').$this->_date."::".$wid,$vid,$vister);
		}
 	}

	/**
	 * ip处理函数
	 */
	function IP($wid,$ip)
	{
		if($this->redisinit->sadd($this->config->item('ip').$wid,$ip))
		{
			$this->redisinit->sadd($this->config->item('dayip').$this->_date."::".$wid,$ip);
		}else{
			$this->redisinit->sadd($this->config->item('indip').$this->_date."::".$wid,$ip);
			$this->redisinit->sadd($this->config->item('dayip').$this->_date."::".$wid,$ip);
		}
	}
	/**
	 * 检测网站
	 * 非本站会员不写入，本站会员当天访问总数+1
	 */
	 function checkWebsite($wid,$vid,$uid,$data)
	 {
		/*更新当天的访问数*/
		$website = $this->redisinit->get('website_'.$uid.'_'.$wid);
		if(!empty($website))
		{
			//非第一次访问
			$new_data = $this->redisinit->get('wid_vid_'.$wid.'_'.$vid);
			if(isset($new_data))
			{
				$new_data['stopTime'] = $data['stopTime']>900?900:$data['stopTime'];
				$new_data['arriveCount'] = $data['arriveCount']+1;
				$this->redisinit->set('wid_vid_'.$wid.'_'.$vid,$new_data,86400);
				return $new_data;
			}else{
				//第一次访问
				$this->redisinit->set('wid_vid_'.$wid.'_'.$vid,$data,86400);
				return null;
			}
		}
		return null;
	 }



  }
?>
